/*   1:    */ package org.intellij.plugins.relaxNG.model.descriptors;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.util.Comparing;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlElement;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.ArrayUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.xml.impl.BasicXmlAttributeDescriptor;
import com.intellij.xml.util.XmlEnumeratedValueReference;
import gnu.trove.THashSet;
import gnu.trove.TObjectHashingStrategy;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kohsuke.rngom.digested.DAttributePattern;
import org.xml.sax.Locator;

import javax.xml.namespace.QName;
import java.util.*;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */ 
/*  32:    */ public class RngXmlAttributeDescriptor
/*  33:    */   extends BasicXmlAttributeDescriptor
/*  34:    */ {
/*  35:    */   @NonNls
/*  36: 44 */   private static final QName a = new QName("", "#unknown");
/*  37: 46 */   private static final TObjectHashingStrategy<Locator> b = new TObjectHashingStrategy()
/*  38:    */   {
/*  39:    */     public int computeHashCode(Locator paramAnonymousLocator)
/*  40:    */     {
/*  41: 48 */       String str = paramAnonymousLocator.getSystemId();
/*  42: 49 */       return paramAnonymousLocator.getLineNumber() * 31 + paramAnonymousLocator.getColumnNumber() * 23 + (str != null ? str.hashCode() * 11 : 0);
/*  43:    */     }
/*  44:    */     
/*  45:    */     public boolean equals(Locator paramAnonymousLocator1, Locator paramAnonymousLocator2)
/*  46:    */     {
/*  47: 52 */       if ((paramAnonymousLocator1.getLineNumber() == paramAnonymousLocator2.getLineNumber()) && (paramAnonymousLocator1.getColumnNumber() == paramAnonymousLocator2.getColumnNumber()) && 
/*  48: 53 */         (Comparing.equal(paramAnonymousLocator1.getSystemId(), paramAnonymousLocator2.getSystemId()))) {
/*  49: 54 */         return true;
/*  50:    */       }
/*  51: 57 */       return false;
/*  52:    */     }
/*  53:    */   };
/*  54:    */   private final Map<String, String> c;
/*  55:    */   private final boolean d;
/*  56:    */   private final RngElementDescriptor e;
/*  57: 64 */   private final THashSet<Locator> f = new THashSet(b);
/*  58:    */   private final QName g;
/*  59:    */   
/*  60:    */   RngXmlAttributeDescriptor(RngElementDescriptor paramRngElementDescriptor, DAttributePattern paramDAttributePattern, Map<String, String> paramMap, boolean paramBoolean)
/*  61:    */   {
/*  62: 68 */     this(paramRngElementDescriptor, a(paramDAttributePattern), paramMap, paramBoolean, new Locator[] { paramDAttributePattern.getLocation() });
/*  63:    */   }
/*  64:    */   
/*  65:    */   private static QName a(DAttributePattern paramDAttributePattern)
/*  66:    */   {
/*  67: 72 */     Iterator localIterator = paramDAttributePattern.getName().listNames().iterator();
/*  68: 73 */     return localIterator.hasNext() ? (QName)localIterator.next() : a;
/*  69:    */   }
/*  70:    */   
/*  71:    */   private RngXmlAttributeDescriptor(RngElementDescriptor paramRngElementDescriptor, QName paramQName, Map<String, String> paramMap, boolean paramBoolean, Locator... paramVarArgs)
/*  72:    */   {
/*  73: 77 */     this.e = paramRngElementDescriptor;
/*  74: 78 */     this.c = paramMap;
/*  75: 79 */     this.d = paramBoolean;
/*  76: 80 */     this.g = paramQName;
/*  77: 81 */     this.f.addAll(Arrays.asList(paramVarArgs));
/*  78:    */   }
/*  79:    */   
/*  80:    */   public RngXmlAttributeDescriptor mergeWith(RngXmlAttributeDescriptor paramRngXmlAttributeDescriptor)
/*  81:    */   {
/*  82: 85 */     QName localQName = paramRngXmlAttributeDescriptor.g.equals(a) ? this.g : paramRngXmlAttributeDescriptor.g;
/*  83:    */     
/*  84: 87 */     HashMap localHashMap = new HashMap(this.c);
/*  85: 88 */     localHashMap.putAll(paramRngXmlAttributeDescriptor.c);
/*  86:    */     
/*  87: 90 */     THashSet localTHashSet = new THashSet(this.f, b);
/*  88: 91 */     localTHashSet.addAll(paramRngXmlAttributeDescriptor.f);
/*  89:    */     
/*  90: 93 */     return new RngXmlAttributeDescriptor(this.e, localQName, localHashMap, (this.d) || (paramRngXmlAttributeDescriptor.d), (Locator[])localTHashSet.toArray(new Locator[localTHashSet.size()]));
/*  91:    */   }
/*  92:    */   
/*  93:    */   public boolean isRequired()
/*  94:    */   {
/*  95: 97 */     return !this.d;
/*  96:    */   }
/*  97:    */   
/*  98:    */   public boolean isFixed()
/*  99:    */   {
/* 100:101 */     return (isEnumerated()) && (this.c.size() == 1);
/* 101:    */   }
/* 102:    */   
/* 103:    */   public boolean hasIdType()
/* 104:    */   {
/* 105:105 */     return this.c.values().contains("ID");
/* 106:    */   }
/* 107:    */   
/* 108:    */   public boolean hasIdRefType()
/* 109:    */   {
/* 110:109 */     return this.c.values().contains("IDREF");
/* 111:    */   }
/* 112:    */   
/* 113:    */   @Nullable
/* 114:    */   public String getDefaultValue()
/* 115:    */   {
/* 116:114 */     return isEnumerated() ? (String)this.c.keySet().iterator().next() : null;
/* 117:    */   }
/* 118:    */   
/* 119:    */   public boolean isEnumerated()
/* 120:    */   {
/* 121:118 */     return (this.c.size() > 0) && (this.c.get(null) == null);
/* 122:    */   }
/* 123:    */   
/* 124:    */   public String[] getEnumeratedValues()
/* 125:    */   {
/* 126:122 */     if (this.c.size() > 0)
/* 127:    */     {
/* 128:    */       Object localObject;
/* 129:124 */       if (this.c.get(null) != null)
/* 130:    */       {
/* 131:125 */         localObject = new HashMap(this.c);
/* 132:126 */         ((Map)localObject).remove(null);
/* 133:    */       }
/* 134:    */       else
/* 135:    */       {
/* 136:128 */         localObject = this.c;
/* 137:    */       }
/* 138:130 */       return (String[])((Map)localObject).keySet().toArray(new String[((Map)localObject).size()]);
/* 139:    */     }
/* 140:132 */     return ArrayUtil.EMPTY_STRING_ARRAY;
/* 141:    */   }
/* 142:    */   
/* 143:    */   public PsiElement getDeclaration()
/* 144:    */   {
/* 145:137 */     Iterator localIterator = this.f.iterator();
/* 146:138 */     if (!localIterator.hasNext()) {
/* 147:138 */       return null;
/* 148:    */     }
/* 149:140 */     return this.e.getDeclaration((Locator)localIterator.next());
/* 150:    */   }
/* 151:    */   
/* 152:    */   public Collection<PsiElement> getDeclarations()
/* 153:    */   {
/* 154:144 */     ContainerUtil.map2List(this.f, new Function()
/* 155:    */     {
/* 156:    */       public PsiElement fun(Locator paramAnonymousLocator)
/* 157:    */       {
/* 158:146 */         return RngXmlAttributeDescriptor.this.e.getDeclaration(paramAnonymousLocator);
/* 159:    */       }
/* 160:    */     });
/* 161:    */   }
/* 162:    */   
/* 163:    */   public String getName(PsiElement paramPsiElement)
/* 164:    */   {
/* 165:153 */     XmlTag localXmlTag = (XmlTag)PsiTreeUtil.getParentOfType(paramPsiElement, XmlTag.class, false, new Class[] { PsiFile.class });
/* 166:    */     String str1;
/* 167:154 */     if (localXmlTag != null)
/* 168:    */     {
/* 169:155 */       str1 = this.g.getNamespaceURI();
/* 170:156 */       String str2 = localXmlTag.getPrefixByNamespace(str1);
/* 171:157 */       if (str2 != null)
/* 172:    */       {
/* 173:158 */         if (str2.length() == 0) {
/* 174:159 */           return this.g.getLocalPart();
/* 175:    */         }
/* 176:161 */         return str2 + ":" + this.g.getLocalPart();
/* 177:    */       }
/* 178:    */     }
/* 179:165 */     if (this.g.getNamespaceURI().length() > 0)
/* 180:    */     {
/* 181:166 */       str1 = this.g.getPrefix();
/* 182:167 */       if ((str1 != null) && (str1.length() > 0)) {
/* 183:168 */         return str1 + ":" + this.g.getLocalPart();
/* 184:    */       }
/* 185:    */     }
/* 186:171 */     return this.g.getLocalPart();
/* 187:    */   }
/* 188:    */   
/* 189:    */   @NonNls
/* 190:    */   public String getName()
/* 191:    */   {
/* 192:176 */     return this.g.getLocalPart();
/* 193:    */   }
/* 194:    */   
/* 195:    */   public void init(PsiElement paramPsiElement) {}
/* 196:    */   
/* 197:    */   public Object[] getDependences()
/* 198:    */   {
/* 199:184 */     return this.e.getDependences();
/* 200:    */   }
/* 201:    */   
/* 202:    */   public String validateValue(XmlElement paramXmlElement, String paramString)
/* 203:    */   {
/* 204:189 */     if (a(paramString)) {
/* 205:190 */       paramString = b(paramString);
/* 206:    */     }
/* 207:192 */     return super.validateValue(paramXmlElement, paramString);
/* 208:    */   }
/* 209:    */   
/* 210:    */   private boolean a(String paramString)
/* 211:    */   {
/* 212:196 */     if (this.c.containsKey(paramString)) {
/* 213:197 */       return "token".equals(this.c.get(paramString));
/* 214:    */     }
/* 215:200 */     paramString = b(paramString);
/* 216:201 */     return (this.c.containsKey(paramString)) && ("token".equals(this.c.get(paramString)));
/* 217:    */   }
/* 218:    */   
/* 219:    */   private static String b(String paramString)
/* 220:    */   {
/* 221:205 */     return paramString.replaceAll("\\s+", " ").trim();
/* 222:    */   }
/* 223:    */   
/* 224:    */   public PsiReference[] getValueReferences(XmlElement paramXmlElement, @NotNull String paramString)
/* 225:    */   {
/* 226:210 */     if (paramString == null) {
/* 227:210 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/intellij/plugins/relaxNG/model/descriptors/RngXmlAttributeDescriptor", "getValueReferences" }));
/* 228:    */     }
/* 229:210 */     new PsiReference[] { new XmlEnumeratedValueReference(paramXmlElement, this)
/* 230:    */     {
/* 231:    */       @Nullable
/* 232:    */       public PsiElement resolve()
/* 233:    */       {
/* 234:214 */         if (RngXmlAttributeDescriptor.this.a(getValue())) {
/* 235:215 */           return getElement();
/* 236:    */         }
/* 237:217 */         return super.resolve();
/* 238:    */       }
/* 239:    */     } };
/* 240:    */   }
/* 241:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor

 * JD-Core Version:    0.7.0.1

 */